//
//  DSYLocalViewController.m
//  BLDMusics
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "DSYLocalViewController.h"
#import "URLTableViewCell.h"

@interface DSYLocalViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *songTableView;
@property (nonatomic, retain) NSMutableArray *songList;

@end

@implementation DSYLocalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self creatTableView];
    
    [self dataHandle];
}



- (void)dataHandle {
    if (self.buttonIndex == 1) {
        
        
        [APPTools GETWithURL:self.url par:nil success:^(id responseObject) {
            
            self.songList = [DSYSongModel modelHandleWithArray:responseObject[@"songs"]];
            
            [DSYDataManager shareDataManager].allDataArray = self.songList;
            
            [_songTableView reloadData];
            
        } filed:^(NSError *error) {
            
        }];
    } else if (self.buttonIndex == 0) {
        
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *plistPath = [documentPath stringByAppendingPathComponent:@"songList.plist"];
        
        NSArray *plistArr = [NSArray arrayWithContentsOfFile:plistPath];
        
        self.songList = [DSYSongModel modelHandleWithArray:plistArr];
        
        [DSYDataManager shareDataManager].allDataArray = self.songList;
        
        self.navigationItem.title = @"本地音乐";
        [_songTableView reloadData];
        
    } else if (self.buttonIndex == 3) {
        self.songList = [NSMutableArray array];
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
            NSURL *plistUrl = [NSURL URLWithString:self.url];
            
            NSArray *array = [NSArray arrayWithContentsOfURL:plistUrl];
        
//        [APPTools GETWithURL:self.url par:nil success:^(id responseObject) {
//            NSLog(@"%@", [responseObject class]);
//        } filed:^(NSError *error) {
//            
//        }];
        
            
            for (NSDictionary *dic in array) {
                DSYSongModel *model = [[DSYSongModel alloc] init];
                model.picUrl = dic[@"picUrl"];
                model.name = dic[@"name"];
                model.lyric = dic[@"lyric"];
                model.singerName = dic[@"singer"];
                model.lyric = dic[@"lyric"];
                model.albumName = dic[@"album"];
                
                DSYUrlModel *urlModel = [[DSYUrlModel alloc] init];
                urlModel.url = dic[@"mp3Url"];
                
                NSMutableArray *urlList = [NSMutableArray array];
                [urlList addObject:urlModel];
                model.urlList = urlList;
                
                [self.songList addObject:model];
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [DSYDataManager shareDataManager].allDataArray = self.songList;
        
        
                // 蓝鸥歌曲
                [_songTableView reloadData];
                
            });
        
        });
        
    }

    
    
    
}

- (void)creatTableView {
    
    self.songTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 124) style:UITableViewStylePlain];
    [self.view addSubview:_songTableView];
    
    _songTableView.delegate = self;
    _songTableView.dataSource = self;
    
    
    [_songTableView registerNib:[UINib nibWithNibName:@"URLTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"urlCell"];
}



#pragma mark - tableView的dataSource和delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DSYSongModel *model = _songList[indexPath.row];
    
    
    URLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"urlCell"];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.picUrl]];
    cell.nameLabel.text = model.name;
    cell.singerLabel.text = model.singerName;
    
    return cell;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%ld", _songList.count);
    return _songList.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DSYMusicViewController *musicVC = [DSYMusicViewController sharedMusicPlayingVC];
    
    musicVC.index = indexPath.row;
    if (self.url.length < 10) {
        musicVC.isLocal = YES;
    }
    
    if (self.buttonIndex == 3) {
        musicVC.isLocal = NO;
    }
    
    
    DSYNAVViewController *nav = [DSYNAVViewController shareNavWithRootViewController:musicVC];
    
    
    [PlayViewController sharePlayViewBar].hidden = YES;
    
    [self presentViewController:nav animated:YES completion:nil];

}


@end
